<template>
  <div class="home_page">

    <h1>我的好友</h1>

    <div class="article_list">
      <div v-for="user in userList" class="article_card" :key="user.id" >
        <div>
          <div class="thumbnail">
            <img :src="user.avatar"/>
          </div>
          <div class="info">
            <div class="title">昵称：{{ user.nickname }}</div>
            <div class="meta">
              <span>邮箱：{{ user.email }}</span>
            </div>
            <div class="meta">
              <span>账号：{{ user.username }}</span>
            </div>
            <div class="meta">
              <span>性别：{{ user.sex==0? '♂':'♀'}}</span>
            </div>
            <div class="btnList">
              <button @click="handleDelete(user.id)">删除好友</button>
              <button @click="handleChat(user.id)">发送消息</button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <h2>{{ userList.length==0? '你没有好友...' : '' }}</h2>

    <h1>好友请求</h1>

    <div class="article_list">
      <div v-for="user in userReqList" class="article_card" :key="user.id" >
        <div>
          <div class="thumbnail">
            <img :src="user.avatar"/>
          </div>
          <div class="info">
            <div class="title">昵称：{{ user.nickname }}</div>
            <div class="meta">
              <span>邮箱：{{ user.email }}</span>
            </div>
            <div class="meta">
              <span>账号：{{ user.username }}</span>
            </div>
            <div class="meta">
              <span>性别：{{ user.sex==0? '♂':'♀'}}</span>
            </div>
            <div class="btnList">
              <button @click="handleAgree(user.id)">同意请求</button>
              <button @click="handleRejuct(user.id)">拒绝请求</button>
            </div>
          </div>
        </div>
      </div>
    </div>



  </div>
</template>
    









<script setup>
import { onMounted, ref } from 'vue'
import hahaPng from '@/assets/xixi.png'
import server from '@/api/server'
import { useRouter } from 'vue-router'
import { getFriendByUser } from '@/api/BlogUser'
import { getFriendReq } from '@/api/BlogUser'
import { delFriend } from '@/api/BlogUser'
import { agreeFriendRea } from '@/api/BlogUser'
import { rejuctFriendRea } from '@/api/BlogUser'
import { userDialogue } from '@/api/BlogUser'

const router = useRouter();

const userList = ref([{
  id: 1,
  avatar: hahaPng,
  email: 1234,
  nickname: 'hahahaha',
  phone: 999,
  username: 999,
  sex: 0
}])

const userReqList = ref([{
  id: 1,
  avatar: hahaPng,
  email: 1234,
  nickname: 'hahahaha',
  phone: 999,
  username: 999,
  sex: 0
}])

onMounted(()=>{
  if(server.token == ''){
    router.push("/notLogin")
    return
  }
  getFriends();
})

const getFriends = ()=>{
  try {
    getFriendByUser(server.token).then((res)=>{
      console.log(res.data)
      if (res.data.code==0) {
        userList.value = res.data.data
        getReq();
      } else {
        alert("获取失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("获取失败："+error)
  }
}

const getReq = ()=>{
  try {
    getFriendReq(server.token).then((res)=>{
      // console.log(res.data)
      if (res.data.code==0) {
        userReqList.value = res.data.data
      } else {
        alert("好友请求获取失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("好友请求获取失败："+error)
  }
}

//删除好友
const handleDelete = (userId)=>{
  const params = {
    friendId: userId
  }
  try {
    delFriend(server.token, params).then((res)=>{
      if (res.data.code==0) {
        alert("好友删除成功："+res.data.data)
        getFriends()
      } else {
        alert("好友删除失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("好友删除失败："+error)
  }
}

const handleAgree = (userId)=>{
  const params = {
    friendId: userId
  }
  try {
    agreeFriendRea(server.token, params).then((res)=>{
      if (res.data.code==0) {
        alert("同意成功："+res.data.data)
        getFriends()
      } else {
        alert("同意失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("同意失败："+error)
  }
}

const handleRejuct = (userId)=>{
  const params = {
    friendId: userId
  }
  try {
    rejuctFriendRea(server.token, params).then((res)=>{
      if (res.data.code==0) {
        alert("拒绝成功："+res.data.msg)
        getFriends()
      } else {
        alert("拒绝失败："+res.data.msg)
      }
    })
  } catch (error) {
    alert("拒绝失败："+error)
  }
}

const handleChat = (chatUserId)=>{
  const params = {
    targetUserId: chatUserId
  }
  try {
    userDialogue(server.token, params).then((res)=>{
      if (res.data.code == 0) {
        router.push('/userDialogue')
      } else {
        alert("获取聊天记录失败："+res.data.msg)
      }
    })
  } catch (error) {
        alert("获取聊天记录失败："+error)
  }
}














</script>
  
  
  
<style scoped>

.home_page {
  display: flex; flex-flow: column nowrap;
  width: 1000px;
  /* align-items: center; */
  margin: 0 auto;padding: 25px;
  background: #f4f4f4;
}

.btnList{
  display: flex;flex-flow: row nowrap;
  justify-content: space-evenly;
  margin-top: 10px;
  width: 200px;
}
.btnList button{
  display: block;  
  /* margin-left: 20px; */
  width: 70px;height: 30px;

  background-color: #ff6699;
  border: none;
  color: white;
  border-radius: 4px;
}



/* 文章列表 */
.article_list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin: 20px 0vw 120px;
}
/* 文章卡片 */
.article_card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 6px rgba(0,0,0,0.05);
  transition: transform 0.2s;
}

.article_card:hover {
  transform: translateY(-4px);
}

/* 缩略图 */
.thumbnail {
  height: 180px;
  overflow: hidden;
  position: relative;
}

.thumbnail img {
  height: 100%;width: 100%;
  object-fit: cover;
}

/* 文章信息 */
.info {
  padding: 8px 10px;
}

.info .title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info .meta {
  font-size: 12px;
  color: #999;
}
</style>